package com.hnu.highlynewapp.service.impl;

import com.hnu.highlynewapp.entity.ContactEntity;
import com.hnu.highlynewapp.service.ContactService;
import com.hnu.highlynewapp.vo.PublishTalentSupplyVo;
import com.hnu.highlynewapp.vo.TalentSupplySearchVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hnu.highlynewapp.utils.PageUtils;
import com.hnu.highlynewapp.utils.Query;

import com.hnu.highlynewapp.dao.TalentSupplyDao;
import com.hnu.highlynewapp.entity.TalentSupplyEntity;
import com.hnu.highlynewapp.service.TalentSupplyService;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;


@Service("talentSupplyService")
public class TalentSupplyServiceImpl extends ServiceImpl<TalentSupplyDao, TalentSupplyEntity> implements TalentSupplyService {

    @Autowired
    private ContactService contactService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<TalentSupplyEntity> page = this.page(
                new Query<TalentSupplyEntity>().getPage(params),
                new QueryWrapper<TalentSupplyEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    @Transactional
    public void publish(PublishTalentSupplyVo vo) {
        ContactEntity contactEntity = new ContactEntity();
        BeanUtils.copyProperties(vo,contactEntity);
        contactEntity.setName(vo.getContactName());
        Long contactId = contactService.saveAndCheckExist(contactEntity);

        TalentSupplyEntity talentSupplyEntity = new TalentSupplyEntity();
        BeanUtils.copyProperties(vo,talentSupplyEntity);
        talentSupplyEntity.setPublishTime(new Date());
        talentSupplyEntity.setName(vo.getTalentName());
        talentSupplyEntity.setContactId(contactId);
        baseMapper.insert(talentSupplyEntity);
    }
    @Override
    public List<TalentSupplyEntity> getListByParma() {

        QueryWrapper<TalentSupplyEntity> wrapper = new QueryWrapper<>();

        wrapper.orderByDesc("publish_time");
        List<TalentSupplyEntity> list = baseMapper.selectList(wrapper);
        return list;
    }
}